package org.mx.dal.hibernate.entity;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

import org.mx.dal.entity.Base;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 基于Hibernate实现的基础实体
 *
 * @author : john.peng date : 2017/10/6
 * @see Base
 */
@MappedSuperclass
@ToString
public class BaseEntity implements Base {
	/**
	 * 
	 */
	private static final long serialVersionUID = -646590113831174103L;

	@Id
	@Column(name = "ID", nullable = false, length = 40)
	private @Getter @Setter String id;

	@Column(name = "CREATED_TIME", nullable = false)
	private @Getter @Setter long createdTime;
	@Column(name = "UPDATED_TIME", nullable = false)
	private @Getter @Setter long updatedTime;
	@Column(name = "OPERATOR", nullable = false, length = 40)
	private @Getter @Setter String operator;
	@Column(name = "VALID", nullable = false)
	private int valid = 1;

	/**
	 * {@inheritDoc}
	 *
	 * @see Object#equals(Object)
	 */
	@Override
	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (o == null || getClass() != o.getClass())
			return false;

		BaseEntity that = (BaseEntity) o;
		if (id != null ? !id.equals(that.id) : that.id != null) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see Object#hashCode()
	 */
	@Override
	public int hashCode() {
		int result = id != null ? id.hashCode() : 0;
		return result;
	}

	/**
	 * 获取是否有效，布尔类型
	 * @return 有效性
	 */
	public boolean isValid() {
		return valid == 1;
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see Base#getValid()
	 */
	@Override
	public int getValid() {
		return valid;
	}

	/**
	 * 设置是否有效，布尔类型
	 * @param valid 有效性
	 */
	public void setValid(boolean valid) {
		this.valid = valid ? 1 : 0;
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see Base#setValid(int)
	 */
	@Override
	public void setValid(int valid) {
		this.valid = valid;
	}
}
